#! /bin/sh

proj_top_dir=`dirname "$0"`
cd "${proj_top_dir}"

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
EXIT() {
	echo ''
	exit 0
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
SET_LOG() {
	LOG_FILE=./log.txt
	rm -f "${LOG_FILE}"
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
LOG_CHECK() {
	if [ -f "${LOG_FILE}" ] ; then
		errcnt=`grep -c 'error:' "${LOG_FILE}"`
		warcnt=`grep -c 'warning' "${LOG_FILE}"`
		echo "${errcnt} Error"
		echo "${warcnt} Warning"

		if [ "${errcnt}" -ne 0 ] ; then
			exit 1
		fi
		if [ "${warcnt}" -ne 0 ] ; then
			exit 1
		fi
	fi
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
REMOVE_LOG() {
	if [ "$1" = "TEST" ] ; then
		return
	fi

	rm -f "${LOG_FILE}"
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
GENERATE_HTMLHELP() {
	echo "*** HTML Help has started"
	rm -f ./boot_strap.chm
	if [ -f "${HCC1_PATH}"/hhc.exe ] ; then
		"${HCC1_PATH}"/hhc.exe ./boot_strap_Specification/index.hhp | tee -a ${LOG_FILE}
	fi
	if [ -f "${HCC2_PATH}"/hhc.exe ] ; then
		"${HCC2_PATH}"/hhc.exe ./boot_strap_Specification/index.hhp | tee -a ${LOG_FILE}
	fi
	rm -rf ./boot_strap_Specification
	echo "*** HTML Help has finished"
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
GENERATE_DOC() {
	echo "*** Doxygen has started"
	rm -rf ./boot_strap_Specification
	if [ -f "${DOXYGEN1_PATH}"/doxygen.exe ] ; then
		"${DOXYGEN1_PATH}"/doxygen.exe ./boot_strap.doxy 2>&1 | tee ${LOG_FILE}
	else
		if [ -f "${DOXYGEN2_PATH}"/doxygen.exe ] ; then
			"${DOXYGEN2_PATH}"/doxygen.exe ./boot_strap.doxy 2>&1 | tee ${LOG_FILE}
		fi
	fi
	echo "*** Doxygen has finished"

	LOG_CHECK

	if [ -f "${HCC1_PATH}"/hhc.exe ] ; then
		GENERATE_HTMLHELP
	fi
	if [ -f "${HCC2_PATH}"/hhc.exe ] ; then
		GENERATE_HTMLHELP
	fi
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
DOXYGEN1_PATH=`cygpath --unix "C:\Program Files\doxygen\bin"`
DOXYGEN2_PATH=`cygpath --unix "C:\Program Files (x86)\doxygen\bin"`
HCC1_PATH=`cygpath --unix "C:\Program Files\HTML Help Workshop"`
HCC2_PATH=`cygpath --unix "C:\Program Files (x86)\HTML Help Workshop"`
SET_LOG
GENERATE_DOC
REMOVE_LOG
EXIT

